<%-- 
    Document   : profile
    Created on : Dec 4, 2011, 3:10:51 PM
    Author     : Michael
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your Profile</title>
<script type="text/javascript">

function button_updateInfo()
{
    if(document.profileInfo.Email.value == "")
        {
            alert("Email cannot be blank!");
            location.reload(true);
        }
    else
    document.profileInfo.submit();
}

function row_circleclick(txt)
{
    document.forms[txt].submit();
}

function row_mousehover(obj)
{
	obj.style.backgroundColor="#6699FF";
}

function row_mouseout(obj)
{
	obj.style.backgroundColor="";
}
</script>
</head>

<body >
<table width="800" border="1">
  <tr>
    <td><img src="Logo.jpg" width="800" height="150" /></td>
  </tr>
  
  <tr>
      <td>
          <input type="button" value="Home" onclick="window.location='gohome.jsp'"/>&nbsp;
          <input type="button" value="Log Out" onclick="window.location='logout.jsp'"/>
          
          <% if(session.getAttribute("Access").equals("Customer")) { %>
            <h1 align="center">Welcome <%=session.getAttribute("User")%>!</h1>
            <% } else { %>
            <p>Current editing profile <%=session.getAttribute("UserID")%></p> <% } %>
                <button onclick="window.location='AdvertisementList.jsp'">Advertisements</button>&nbsp;
                <button onclick="window.location='CircleList.jsp'">All Circles</button>&nbsp;
                <button onclick="window.location='suggestions.jsp'">Item Suggestions</button>&nbsp;
                <input type="button" value="Search Circles" onclick="window.location='SearchCircles.jsp'"/>&nbsp;
                <input type="button" value="Search Users" onclick="window.location='SearchPeople.jsp'"/>&nbsp;
         
           <% if(session.getAttribute("Access").equals("Customer")) { %>
                <button onclick="window.location='MessageIndex.jsp'">Messages</button>
                <button onclick="window.location='joinRequests.jsp'">Join Requests</button>
            <% } %>
            <% if(session.getAttribute("Access").equals("Manager")) { %>
                <button onclick="var r = confirm('Really Delete User?'); if(r==true) window.location='DeleteUser.jsp'">***DELETE USER***</button>
            <% } %>
    </td>
   </tr>
   
   <tr>
    <td>
        <table width="800" >
        <tr>
        <td width="400" align="right" >
        <% 
            java.sql.Connection conn = null;
            try
            {
                conn = new DBClasses.GetConnection().getConnection();
                String query = "SELECT * FROM Customer WHERE CustomerID='" + session.getAttribute("UserID") + "'";

                java.sql.Statement stmt = conn.createStatement();
                java.sql.ResultSet rs = stmt.executeQuery(query);

                if(!rs.next())
                {
                    response.sendRedirect("employee.jsp");
                }
        %>
        <form style="padding-right: 100px;" name="profileInfo" id="profileInfo" action="userInfo.jsp" method="post">
            <h3>Your Profile</h3>
            <label>First Name: </label>
            <input type="text" name="Firstname" maxlength="50" value="<%=rs.getString("Firstname") %>"/><br />
            
            <label>Last Name: </label>
            <input type="text" name="Lastname" maxlength="50" value="<%=rs.getString("Lastname") %>"/><br />
            
            <% String gend = rs.getString("Sex");
               String[] selectedyn = new String[]{"",""};
               if(gend.equalsIgnoreCase("M")) { selectedyn[0] = "selected=\"selected\""; } 
               else { selectedyn[1] = "selected=\"selected\"";  } %>
               
            <label>Gender: </label>
            <select style="width: 142px;" name="Sex">
                <option value="M" <%=selectedyn[0]%>>Male</option>
                <option value="F" <%=selectedyn[1]%>>Female</option>
            </select><br />
            
            <label>Email: </label>
            <input type="text" name="Email" maxlength="50" value="<%=rs.getString("Email") %>"/><br />
            
            <label>Date of Birth: </label>
            <input type="text" name="Dob" maxlength="20" value="<%=rs.getString("Dob") %>"/><br />
            
            <label>Address: </label>
            <input type="text" name="Address" maxlength="100" value="<%=rs.getString("Address") %>"/><br />
            
            <label>City: </label>
            <input type="text" name="City" maxlength="20" value="<%=rs.getString("City") %>"/><br />
            
            <label>State: </label>
            <input type="text" name="State" maxlength="2" value="<%=rs.getString("State") %>"/><br />
            
            <label>Zip: </label>
            <input type="text" name="Zip" maxlength="5" value="<%=rs.getString("Zip") %>"/><br />
            
            <label>Telephone: </label>
            <input type="text" name="Telephone" maxlength="10" value="<%=rs.getString("Telephone") %>"/><br />
            
            <label>Credit Card: </label>
            <input type="text" name="CreditCard" maxlength="16" value="<%=rs.getString("CreditCard") %>"/><br />
            
            <label>Preferences: </label>
            <input type="text" name="Preferences" maxlength="80" value="<%=rs.getString("Preferences") %>"/><br />
            
            <input type="button" value="Update Info" onclick="return button_updateInfo()" />
        </form>
    </td>
    <td width=".5" bgcolor="#999999" />
        <td align="center" valign="top"><h3>Your Circles</h3>
            <table width="400" border="1">
              <tr>
                  <td width="52" />
                  <td width="174">Circle Name</td>
                  <td width="174">Circle Type</td>
                 
              </tr>
          <%
                query = "SELECT CircleID FROM circlemembership WHERE CustomerID='" + session.getAttribute("UserID") + "'";
                rs = stmt.executeQuery(query);
                java.sql.Statement stmt2 = conn.createStatement();
                int i=0;
                while(rs.next())
                {

                    String circleID = Integer.toString(rs.getInt("CircleID"));
                    String query2 = "SELECT CircleName, CircleType, CircleOwner FROM circle WHERE CircleID='" + circleID + "'";
                    java.sql.ResultSet rs1 = stmt2.executeQuery(query2);  
                    if(rs1.next())
                   {
          %>

        <tr onclick="row_circleclick('<%="circle" + i %>')" onmouseout="row_mouseout(this)" onmouseover="row_mousehover(this)">
            <form name="<%="circle" + i %>" action="CirclePage.jsp" method="post">
                
            <input name="CIRC" type="hidden" value="<%=circleID%>" />
                <td width="52">
                <img src="CircleLogo.png" alt="" width="52" height="52" />
                </td><td width="174"  valign="middle">
                    <label name="circlename" ><%= rs1.getString("CircleName")  %></label> </td>
                    <td width="174" valign="middle"><label name="circletype" > <%= rs1.getString("CircleType") %> </label>
                </td>
            </form>
            </tr>

                <%
                           }
                        i++;
                       }
                   }
                   catch(Exception e)
                   {
                       e.printStackTrace();
                   }
            
                   finally
                   {
                       try{
                       conn.close(); }
                       catch(Exception e) {
                           e.printStackTrace();
                       }
                   }
                %>
                
                <% if(session.getAttribute("Access").equals("Customer")) { %>
          <tr >
            <form name="newCircle" action="circleInfo.jsp" method="post">
                <input name="CIRC" type="hidden" value="-1" />
                <td width="52">
                    <input type="button" value="New" 
                           onclick="if(document.newCircle.circlename.value=='' || 
                           document.newCircle.circletype.value=='')
                           {
                               alert('Missing fields!');
                           }
                           else
                                document.newCircle.submit()" />
                </td><td width="174"  valign="middle">
                    <input type="text" name="circlename"/>  </td>
                <td width="174" valign="middle"><input type ="text" name="circletype" maxlength="10" /> 
                </td>
            </form>
            </tr>
          <% } %>
          
          </table>
          
                </td>
    </tr>
    </table>
    </td>
    
  </tr>
</table>
</body>
</html>